package com.zw.hrm.service.impl;

import com.netflix.discovery.converters.Auto;
import com.zw.hrm.doc.CourseDoc;
import com.zw.hrm.domain.Course;
import com.zw.hrm.domain.CourseDetail;
import com.zw.hrm.domain.CourseMarket;
import com.zw.hrm.dto.CourseSaveDTO;
import com.zw.hrm.feignclients.ESFeignClient;
import com.zw.hrm.mapper.CourseDetailMapper;
import com.zw.hrm.mapper.CourseMapper;
import com.zw.hrm.mapper.CourseMarketMapper;
import com.zw.hrm.query.CourseQuery;
import com.zw.hrm.service.ICourseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zw.hrm.util.AjaxResult;
import com.zw.hrm.util.LoginInfo;
import com.zw.hrm.util.PageList;
import com.zw.hrm.util.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zw
 * @since 2020-02-21
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseDetailMapper courseDetailMapper ;

    @Autowired
    private CourseMarketMapper courseMarketMapper ;

    @Autowired
    private ESFeignClient esFeignClient;

    @Override
    public void saveOrUpdate(CourseSaveDTO courseSaveDTO) {
        //1.保存课程
        Course course = courseSaveDTO.getCourse();

        //课程状态
        course.setStatus(Course.STATUS_OFFLINE);
        //开课时间
        //假数据
        Date date = new Date();
        course.setStartTime(date);
        //节课时间
        course.setEndTime(date);

        //等级名字： ok
        //登录对象============
        //机构id :
        LoginInfo loginInfo = UserContext.getLoginInfo();
        course.setTenantId(loginInfo.getTenantId());
        //机构名字
        course.setTenantName(loginInfo.getTenantName());
        //创建课程的用户id
        course.setUserId(loginInfo.getUserId());

        //创建课程的用户名
        course.setUserName(loginInfo.getUserName());

        baseMapper.insert(course);

        //2.保存课程详情
        CourseDetail courseDetail = courseSaveDTO.getCourseDetail();
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);

        //3.保存课程营销
        CourseMarket courseMarket = courseSaveDTO.getCourseMarket();

        courseMarket.setId(course.getId());
        courseMarket.setPriceOld(courseMarket.getPrice());

        //截止时间:假数据
        courseMarket.setExpires(date);

        courseMarketMapper.insert(courseMarket);

    }

    /**
     *         //3.1.es-server需要编写controller接口
     *             //注意：controller中参数接受CourseDoc
     *             //需要把course ,courseMarket 等等相关数据封装成 CourseDoc
     *             //问题？那么course-server 是不是要依赖 es-server？no
     *             //我们可以把CurseDoc 抽成公共的模块，course-server去依赖 ， es-server也去依赖
     *         //3.2.需要编写ES的Feign
     * @param id
     */
    @Override
    public void onLineCourse(Long id) {
        //1.根据id查询课程
        Course course = baseMapper.selectById(id);

        //2.判断课程的状态
        if(course.getStatus() != Course.STATUS_OFFLINE){
            throw new RuntimeException("课程状态不处于下线状态");
        }

        //3.把课程添加到ES中
        //3.3.course-server通过Feign调用es-server

        //需要把course ,courseMarket 等等相关数据封装成 CourseDoc
        CourseDetail courseDetail = courseDetailMapper.selectById(id);
        CourseMarket courseMarket = courseMarketMapper.selectById(id);

        CourseDoc courseDoc = new CourseDoc();
        BeanUtils.copyProperties(course,courseDoc);
        BeanUtils.copyProperties(courseDetail,courseDoc);
        BeanUtils.copyProperties(courseMarket,courseDoc);

        AjaxResult ajaxResult = esFeignClient.save(courseDoc);
        if(ajaxResult.isSuccess()){
            //4.修改Mysql中的课程状态为：上线
            course.setStatus(Course.STATUS_ONLINE);
            baseMapper.updateById(course);
        }
    }

    @Override
    public void offLineCourse(Long id) {
        Course course = baseMapper.selectById(id);
        if (course == null && course.getStatus().longValue() != Course.STATUS_ONLINE){
            throw new RuntimeException("课程不存在或课程状态未上线");
        }

        //删除课程
        AjaxResult result = esFeignClient.deleteById(id);

        //判断ES中课程是否删除成功
        if (!result.isSuccess()){
            throw new RuntimeException("课程下线失败");
        }

        //将数据库中课程状态转为下线
        course.setStatus(Course.STATUS_OFFLINE);
        baseMapper.updateById(course);
    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        return esFeignClient.searchCourse(courseQuery);
    }
}
